package com.situ.mall.service.impl;

import com.situ.mall.mapper.CartMapper;
import com.situ.mall.pojo.Cart;
import com.situ.mall.pojo.vo.CartVO;
import com.situ.mall.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements ICartService {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private CartMapper cartMapper;

    @Override
    public boolean insert(Cart cart) {
        int count = 0;
        Cart findCart = new Cart();
        findCart.setUserId(cart.getUserId());
        findCart.setProductId(cart.getProductId());
        Integer selectCount = cartMapper.selectByUserAndProduct(findCart);
        if (selectCount == null || selectCount == 0) {
            count = cartMapper.insertSelective(cart);
        } else {
            count = cartMapper.updateByUserAndProduct(cart);
        }
        return count > 0;
    }

    @Override
    public List<CartVO> selectByUserId(Integer id) {
        return cartMapper.selectByUserId(id);
    }

    @Override
    public boolean deleteById(Integer id) {
        return cartMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean UpdateChecked(Integer id, Integer checked) {
        return cartMapper.updateChecked(id, checked) > 0;
    }

    @Override
    public List<CartVO> selectByUserIdAndChecked(Integer id) {
        return cartMapper.selectByUserIdAndChecked(id);
    }
}
